package com.zy.socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/16 11:01
 **/
public class Client1 {
    public static void main(String[] args) {
        try {
            final Socket socket = new Socket("192.168.5.47",8989);
            //读入键盘
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            //socket写出
            DataOutputStream writer = new DataOutputStream(socket.getOutputStream());

           //启动监听服务器的信息
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //sockeet读入,启动一条线程监听读入
                    try {
                        InputStream is = socket.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                        BufferedReader br = new BufferedReader(isr);
                        while (true) {
                            System.out.println(br.readLine());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            //读入键盘信息后写出到socket中
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            PrintWriter pw = new PrintWriter(osw, true);
            Scanner scanner = new Scanner(System.in);
            while (true) {
                pw.println(scanner.nextLine());
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
